2;
/**
 * create by young (young2f@outlook.com) on 2024/4/21
 */
'use strict';
const base = require('../base/controllerBase');
const _stringUtils = require('../utils/stringUtils');
const { code } = require('../base/modelBase');
const _ = require('lodash');
const table = 'DicPic';

class PersonnelController extends base {
  async createPersonnel() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        name: { type: 'string' },
      };

      ctx.validate(model, body);
      const { code } = body;
      body.code = code ? code : _stringUtils.getCode('PER');
      const result = await ctx.app.dbs.insert('Personnel', body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deletePersonnel() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const { code, tenantCode } = body;
      const result = await ctx.app.dbs.deleteOne('Personnel', { code, tenantCode });
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updatePersonnel() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const { code, tenantCode } = body;
      const result = await ctx.app.dbs.updateOne('Personnel', { code, tenantCode }, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findPersonnelMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      const { code, tenantCode, name } = body;
      const query = { tenantCode };
      if (name) query.name = name;
      if (code) query.code = code;
      const result = await ctx.app.dbs.find('Personnel', query);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async createDic() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        table: { type: 'string' },
      };
      ctx.validate(model, body);
      ctx.body = await service['personnelService'].createDic(body);
      ctx.status = 200;

    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }


  async deleteDic() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        table: { type: 'string' },
        recordId: { type: 'string' },
      };

      ctx.validate(model, body);
      const { table, recordId, tenantCode } = body;
      const result = await ctx.app.dbs.deleteOne(table, { recordId, tenantCode });
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateDic() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        recordId: { type: 'string' },
        table: { type: 'string' },
      };

      ctx.validate(model, body);
      const { table, recordId, tenantCode } = body;
      console.log('更新', body);
      const result = await ctx.app.dbs.updateOne(table, { recordId, tenantCode }, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findDicMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        table: { type: 'string' },
      };
      console.log('find dic many bo ', body);
      ctx.validate(model, body);
      ctx.body = await service['personnelService'].findDicMany(body);
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async exportExcel() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      ctx.body = await service['personnelService'].exportExcel(body);
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

}

module.exports = PersonnelController;
